import axios from "axios";
import { message } from "ant-design-vue";
import NProgress from "nprogress";
import { Config } from "@config/index";
const config = {
  baseURL: Config.baseURL, // 服务器地址或配置的代理的路径
  timeout: 10000, // 超时时间
  headers: {}, // 配置请求头
};

const setInterceptors = (axiosInstance) => {
  // 请求拦截
  axiosInstance.interceptors.request.use(
    (request) => {
      NProgress.start();
      console.log("请求入参：", request.url, request.data);
      return request;
    },
    (error) => {
      NProgress.start();
      message.warning("入参有误,请进行校验");
      // Promise.reject(error);
    }
  );

  // 响应拦截
  axiosInstance.interceptors.response.use(
    (response) => {
      NProgress.done();
      // 网络请求结果
      if (response.data) {
        // 内部接口处理
        let { code, message, ...args } = response.data;
        console.log(args);
        if (code == 200) {
          return args;
        } else {
          message.error(message);
        }
      } else {
        message.error("请求未返回数据");
      }
    },
    (error) => {
      NProgress.done();
      console.log(error);
      let { response } = error;
      message.error(response.statusText);
      // if(response.status == '404'){

      // }else{

      // }
      // return Promise.reject(error.response);
    }
  );
};

const axiosInstance = axios.create(config);

setInterceptors(axiosInstance);

export default axiosInstance;
